package com.chookss.home.knowledge;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.chookss.R;
import com.chookss.tools.Utils;
import com.chookss.view.BasisTimesUtils;
import com.chookss.view.ColorFlipPagerTitleView;
import com.gyf.immersionbar.ImmersionBar;
import com.lvgroup.hospital.base.BaseAct;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.AttachPopupView;
import com.lxj.xpopup.interfaces.OnSelectListener;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import org.jetbrains.annotations.Nullable;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ScoreRankActivity extends BaseAct {


    @BindView(R.id.ivTop)
    ImageView ivTop;
    @BindView(R.id.rlTop)
    RelativeLayout rlTop;
    @BindView(R.id.indicator_view)
    MagicIndicator indicatorView;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.tvTime)
    TextView tvTime;
    @BindView(R.id.ivTimeInit)
    ImageView ivTimeInit;
    @BindView(R.id.llTime)
    LinearLayout llTime;
    private String[] title = { "知识贡献", "日常办公", "技能提升","能量贡献"};
    private MyAdapter pagerAdapter;
    private ArrayList<ScoreRankFragment> fragments = new ArrayList<>();
    private String currentDate = "";//选择时间
    AttachPopupView attachPopupView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score_rank);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        ImmersionBar.with(this)
                .transparentStatusBar()  /*透明状态栏，不写默认透明色*/  /*状态栏字体是深色，不写默认为亮色*/
                .statusBarDarkFont(true)
                .flymeOSStatusBarFontColor(R.color.grey_3)  /*修改flyme OS状态栏字体颜色*/
                .keyboardEnable(false).navigationBarEnable(false)
                .init();
        int statusBarHeight = Utils.getStatusBarHeight(this);
        RelativeLayout.LayoutParams rlTopParams = (RelativeLayout.LayoutParams) rlTop.getLayoutParams();
        rlTopParams.setMargins(0, statusBarHeight, 0, 0);
        rlTop.setLayoutParams(rlTopParams);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        currentDate = sf.format(c.getTime());
        tvTime.setText(Utils.changeCnDate(currentDate));
        fragments.add(new ScoreRankFragment("3"));
        fragments.add(new ScoreRankFragment("1"));
        fragments.add(new ScoreRankFragment("2"));
        fragments.add(new ScoreRankFragment("0"));
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setScrollPivotX(0.65f);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return title == null ? 0 : title.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorFlipPagerTitleView(context);
                simplePagerTitleView.setIncludeFontPadding(false);
                simplePagerTitleView.setText(title[index]);
                simplePagerTitleView.setTextSize(15);
                simplePagerTitleView.setNormalColor(Color.rgb(194, 135, 72));
                simplePagerTitleView.setSelectedColor(Color.rgb(111, 73, 53));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (viewPager == null) {
                            return;
                        }
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(UIUtil.dip2px(context, 2));
                indicator.setLineWidth(UIUtil.dip2px(context, 42));
                indicator.setRoundRadius(UIUtil.dip2px(context, 1));
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
                indicator.setColors(Color.rgb(111, 73, 53));
                return indicator;
            }
        });
        indicatorView.setNavigator(commonNavigator);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                indicatorView.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                indicatorView.onPageSelected(position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                indicatorView.onPageScrollStateChanged(state);
            }
        });
        pagerAdapter = new MyAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOffscreenPageLimit(3);
        getData();
    }

    private void getData() {
        for (int i = 0; i < fragments.size(); i++) {
            fragments.get(i).setRefresh(currentDate);
        }
    }

    class MyAdapter extends FragmentStatePagerAdapter {
        public MyAdapter(@NonNull FragmentManager fm, List<ScoreRankFragment> fragments) {
            super(fm);
        }

        @NonNull
        @Override
        public ScoreRankFragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

    int mYear = 0, mMonth = 0, mDay = 0;
    String monthS = "", dayS = "";

    private void showDatePickDialog(int state) {
        //state1 按月选择 2 按日期
        Calendar calendar = Calendar.getInstance();
        if (mYear == 0) {
            mYear = calendar.get(Calendar.YEAR);
            mMonth = calendar.get(Calendar.MONTH);
            mDay = calendar.get(Calendar.DAY_OF_MONTH);
        }
        if (tvTime == null) {
            return;
        }
        if (state == 1) {
            BasisTimesUtils.showDatePickerDialog(this, true, "请选择月份", mYear, mMonth, mDay,
                    new BasisTimesUtils.OnDatePickerListener() {

                        @Override
                        public void onConfirm(int year, int month, int dayOfMonth) {
                            mYear = year;
                            mMonth = month - 1;
                            mDay = dayOfMonth;
                            if (month > 9) {
                                monthS = month + "";
                            } else {
                                monthS = "0" + month;
                            }
                            currentDate = mYear + "-" + monthS;
                            tvTime.setText(Utils.changeCnDate(currentDate));
                            getData();
                        }

                        @Override
                        public void onCancel() {
                        }
                    }).setDayGone();
        } else {
            BasisTimesUtils.showDatePickerDialog(this, true, "请选择日期", mYear, mMonth, mDay,
                    new BasisTimesUtils.OnDatePickerListener() {

                        @Override
                        public void onConfirm(int year, int month, int dayOfMonth) {
                            mYear = year;
                            mMonth = month - 1;
                            mDay = dayOfMonth;
                            if (dayOfMonth > 9) {
                                dayS = dayOfMonth + "";
                            } else {
                                dayS = "0" + dayOfMonth;
                            }
                            if (month > 9) {
                                monthS = month + "";
                            } else {
                                monthS = "0" + month;
                            }
                            currentDate = mYear + "-" + monthS + "-" + dayS;
                            tvTime.setText(Utils.changeCnDate(currentDate));
                            getData();
                        }

                        @Override
                        public void onCancel() {
                        }
                    }).setDay(0);
        }

    }

    @OnClick({R.id.title_back, R.id.llTime})
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.llTime:
                attachPopupView = new XPopup.Builder(this)
                        .hasShadowBg(false)
                        .isClickThrough(true)
//                            .popupPosition(PopupPosition.Bottom) //手动指定弹窗的位置
                        .atView(llTime)  // 依附于所点击的View，内部会自动判断在上方或者下方显示
                        .asAttachList(new String[]{"全部", "按月份查询", "按日期查询"},
                                null,
                                new OnSelectListener() {
                                    @Override
                                    public void onSelect(int position, String text) {
                                        if (position == 0) {
                                            currentDate = "";
                                            tvTime.setText("全部");
                                            getData();
                                        } else if (position == 1) {
                                            showDatePickDialog(1);
                                        } else if (position == 2) {
                                            showDatePickDialog(2);
                                        }
                                    }
                                }, 0, 0, Gravity.CENTER);
                ;
                attachPopupView.show();
                break;
        }
    }
}
